const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    topTips: false,
    hide: false,
    topMsg:'',
    config:{},
  },
  onShow: function(){
    app.appConfig((rel)=>{
      if(rel.is_store == 0){
        app.wxAlert('非商家身份',()=>{
          wx.navigateBack({
            delta:1
          })
        })
      }else{
        this.setData({
          config:rel
        })
      }
    });  
  },
  //保存图片
  savephone:function(){
    var that = this;
    //判断图片权限
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.writePhotosAlbum']) {
          that.saveToPhotos();
        }else{
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success(res) {
              that.saveToPhotos();
            }
          })
        }
      },
    })
  },
  saveToPhotos:function(){
    var that = this;
    wx.downloadFile({
      url:that.data.config.mch_qrcode,
      success:function(res){
        let path = res.tempFilePath
        wx.saveImageToPhotosAlbum({
          filePath: path,
          success(res) {
            that.setData({topMsg:'保存到相册成功'});
            that.topTips();
          },fail(res) {
            that.setData({topMsg:'保存到相册失败'});
            that.topTips();
          }
        })
      },fail:function(res){
        that.setData({topMsg:'保存到相册失败'});
        that.topTips();
      }
    })
  },
  //提示显示
  topTips: function() {
    this.setData({topTips: true});
    setTimeout(() => {
      this.setData({topTips: false,hide: false,});
    },2000);
  }
})